define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'review/index',
                    add_url: 'review/add',
                    edit_url: 'review/edit',
                    del_url: 'review/del',
                    multi_url: 'review/multi',
                    table: 'review',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
						{field: 'bar_code', title: __('Bar_code')},
						{field: 'sales', title: __('Sales')},
						{field: 'guige', title: __('Guige')},
						{field: 'product_name', title: __('Product_name')},
                        {field: 'chuku', title: __('Chuku')},
						{field: 'note', title: __('Note')},
						{field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {'normal': __('Normal'), 'hidden': __('Hidden')}, style: 'min-width:100px;'},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
    {name: 'run', text: '二维码', title: '二维码', icon: 'fa fa-download', classname: 'btn btn-xs btn-success btn-run-amor', url: 'review/qrcode'}
], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                //普通搜索
                commonSearch: true,
                titleForm: '', //为空则不显示标题，不定义默认显示：普通搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
				//
				$(document).on("click", "#fieldlist .append", function () {
                    var rel = parseInt($(this).closest("dl").attr("rel")) + 1;
                    $(this).closest("dl").attr("rel", rel);
					var data_field = $(this).closest("dl").attr("data-field");
                    $('<dd class="form-inline"><input type="text" name="field[' + data_field + '][' + rel + ']" class="form-control" id="field-' + rel + '" value="" size="10" /> <input type="text" name="value[' + data_field + '][' + rel + ']" class="form-control" id="value-' + rel + '" value="" size="40" /> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span></dd>').insertBefore($(this).parent());
                });
                $(document).on("click", "#fieldlist dd .btn-remove", function () {
                    $(this).parent().remove();
                });
                //拖拽排序
                require(['dragsort'], function () {
                    //绑定拖动排序
                    $("dl#fieldlist").dragsort({
                        itemSelector: 'dd',
                        dragSelector: ".btn-dragsort",
                        dragEnd: function () {

                        },
                        placeHolderTemplate: "<dd></dd>"
                    });
                });
				//
            }
        }
    };
    return Controller;
});

$(document).on("click", "button.readcache", function () {
	var html;
	$.ajax({
	    url: $('button.readcache').attr('data-url'),
		type: 'get',
		data: '',
		cache:false,  
		dataType: 'json',
		success: function(d) {
		    if(d.success){
				$.each(d.data, function(index, content)
                { 
                    if(typeof(content)=="object"){
						var i = 1;
						$.each(content, function(index2, content2)
                        {
							i++;
							html = '<dd class="form-inline"><input type="text" name="field['+index+']['+i+']" class="form-control" id="field-'+i+'" value="'+index2+'" size="10"> <input type="text" name="value['+index+']['+i+']" class="form-control" id="value-'+i+'" value="'+content2+'" size="40"> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span></dd>';
							$("dl[data-field='"+index+"'] dd:eq(0)").after(html);
						});
						$("dl[data-field='"+index+"']").attr('rel',i);
				    }else{
						$("input[name='row["+index+"]']").val(content);
				    }
                });
 			}
		}
	});	
});
$(document).on("click", "button.writecache", function () {
	var data = $("form").serialize();
    $.ajax({
		    url: $('button.writecache').attr('data-url'),
		    type: 'post',
		    data: data,
		    cache:false,  
		    dataType: 'json',
		    success: function(d) {
		        if(d.error){
				}else if(d.success){
				}
		    }
	});			
});